var app = getApp();
Page({
  data: {
    // applyStatus:0
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.url){
      console.log(decodeURIComponent(e.url));
      that.setData({
        jumpUrl: decodeURIComponent(e.url)
      })
    }
    // that.requestInfo();
    // var url = encodeURIComponent('/subpages1/goodDetail/goodDetail?id=123');
    // console.log(url);
    if (!app.globalData.plumSession) {
      console.log("暂未获取到session");
      app.wechatSq(that);
    } else {
      that.requestInfo();
    }
  },
  onPullDownRefresh:function(){
    var that = this;
    that.requestInfo();
  },
  requestInfo:function(){
    var that = this;
    // 判断是否验证
    var data = {
      map: 'applet_start_page'
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data.data);
        var applyInfo = res.data.data;
        that.setData({
          applyInfo: applyInfo,
          applyStatus: applyInfo.applyStatus,
          applyTip: applyInfo.applyTip
        })
        console.log("验证状态"+(applyInfo.applyStatus==2))
        if (applyInfo.applyStatus==2){
          var jumpUrl = that.data.jumpUrl;
          wx.reLaunch({
            url: jumpUrl ? jumpUrl:applyInfo.indexUrl
          })
        }else{
          var applyTtitle = applyInfo.applyTtitle ? applyInfo.applyTtitle : '申请验证';
          console.log(applyTtitle);
          app.setNavtitle(applyTtitle);
        }
      },
      fail: function (res) {
        console.log(res.data);
        app.errorTip(that, res.data.em, 2000);
      },
      complete:function(){
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  inputChange:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]:e.detail.value
    })
    console.log(that.data.type)
  },
  submitInfo: function () {
    var that = this;
    app.getSubId(that, 'applet_member_mobile_apply', 'toSubmitInfo');
  },
  toSubmitInfo: function () {
    var that = this;
    // 判断是否验证
    var data = {
      map: 'applet_member_mobile_apply',
      name: that.data.name ? that.data.name : '',
      mobile: that.data.mobile ? that.data.mobile : ''
    }
    if (!data.name) {
      app.errorTip(that, "请输入姓名", 2000);
      return;
    }
    let reg = /1\d{10}/;
    if (!reg.test(data.mobile)) {
      app.errorTip(that, "请输入正确的手机号码", 2000);
      return;
    }
    console.log(data);
    wx.showModal({
      title: '确认提交？',
      content: '提交后请耐心等待审核。',
      success(obj) {
        if (obj.confirm) {
          wx.showLoading({
            title: '正在提交',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                wx.showModal({
                  title: '',
                  content: res.data.data.applyMsg,
                  showCancel: false,
                  success: function () {
                    that.requestInfo();
                  }
                })
              } else {
                app.errorTip(that, res.data.em, 2000);
              }

            },
            fail: function (res) {
              app.errorTip(that, res.data.em, 2000);
            },
            complete: function () {
              wx.hideLoading();
              wx.stopPullDownRefresh();
            }
          });
        }
      }
    })

  },
  newapply:function(){
    var that = this;
    that.setData({
      applyStatus:0
    })
  }
})